package edu.zjut.sjq.controller;

import edu.zjut.sjq.bean.Apply;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.service.ApplyService;
import edu.zjut.sjq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by sue on 2017/3/18.
 */
@Controller
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private UserService userService;

    /**
     * 申请更改身份
     * @param apply
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateRole",produces = "application/json")
    public Result updateRole(@RequestBody Apply apply){
        return applyService.updateRole(apply);
    }

    /**
     * 获取所有未处理申请信息
     * @return
     */
    @RequestMapping("/getAll")
    public ModelAndView getAllApply(){
        List<Apply> applyList = applyService.getAllApply();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("applyList.jsp");
        modelAndView.addObject("applyList",applyList);
        return modelAndView;
    }

    /**
     * 获取审批详情
     * @return
     */
    @RequestMapping(value = "/getDetail",produces = "application/json;charset=UTF-8")
    public ModelAndView getDetail(@RequestParam("applyId") Integer applyId){
        Apply apply = applyService.selectById(applyId);
        User applyUser = userService.selectById(apply.getUserId());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("reviewApply.jsp");
        modelAndView.addObject("apply",apply);
        modelAndView.addObject("applyUser",applyUser);
        return modelAndView;
    }

    /**
     * 审批申请
     * @return
     */
    @ResponseBody
    @RequestMapping("/review")
    public Result reviewApply(@RequestBody Apply apply){
        return applyService.reviewApply(apply);
    }

}
